package com.vm.adapter;

import java.util.List;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.activity.BitmapCache;
import com.vm.activity.DetailActivity;
import com.vm.commons.StringUtil;
import com.vm.commons.VMConst;
import com.vm.model.ActiveSoft;
import com.vm.model.Soft;
import com.vm.myview.FoundDown;
import com.vm.utils.LruImageCache;
import com.vm.utils.MyDisplayImageOptions;
import com.vm.utils.MyVolley;
import com.vm.utils.SingleRequestQueue;
import com.wwt.app.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年2月2日 上午10:32:21]
 * @Version: [v1.0]
 * 
 */
public class FoundAdapter extends BaseAdapter {

	private Context mContext;
	// private ImageLoader mImageLoader;
	private List<ActiveSoft> listSofts;
	boolean isFirstZero = true;

	/**
	 * 
	 */
	public FoundAdapter(Context context, List<ActiveSoft> lists) {
		this.mContext = context;
		this.listSofts = lists;

		// RequestQueue requestQueue =
		// Volley.newRequestQueue(context.getApplicationContext());

		// mImageLoader = MyVolley.getImageLoader();
	}

	@Override
	public int getCount() {
		return listSofts.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.found_gv_item, null);
			viewHolder = new ViewHolder();

			/**
			 * 获取各个需要显示属性的控件
			 */

			viewHolder.tv_softName = (TextView) convertView
					.findViewById(R.id.tv_softName);

			viewHolder.iv_softImage = (ImageView) convertView
					.findViewById(R.id.iv_softImage);
			// viewHolder.iv_softImage = (NetworkImageView) convertView
			// .findViewById(R.id.iv_softImage);

			viewHolder.tv_softSize = (TextView) convertView
					.findViewById(R.id.tv_softSize);
			viewHolder.tv_download_num = (TextView) convertView
					.findViewById(R.id.tv_download_num);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (position == 0) {
			if (isFirstZero) {
				isFirstZero = false;
			} else {
				return convertView;
			}

		} else {
			isFirstZero = true;
		}

		Log.i("FoundAdapter", "position:" + position);

		ActiveSoft activeSoft = listSofts.get(position);

		viewHolder.tv_softName.setTag(activeSoft.getSoftId());
		// 名称

		if (activeSoft.getName() != null) {
			String softName = StringUtil.getShotName(activeSoft.getName());
			viewHolder.tv_softName.setText(softName);
		}
		// 大小
		if (activeSoft.getSize() != null) {
			float size = activeSoft.getSize();
			viewHolder.tv_softSize.setText(Float.toString(size) + "M");
		}

		if (activeSoft.getDownload() != null) {
			String download = null;
			if (activeSoft.getDownload() > 10000) {
				download = Integer.toString(activeSoft.getDownload() / 10000)
						+ "."
						+ Integer
								.toString((activeSoft.getDownload() % 10000) / 1000)
						+ "万";
			} else if (activeSoft.getDownload() > 100000000) {
				download = Integer
						.toString(activeSoft.getDownload() / 100000000)
						+ "."
						+ Integer
								.toString((activeSoft.getDownload() % 100000000) / 10000000)
						+ "亿";
			} else {
				download = Integer.toString(activeSoft.getDownload());
			}
			viewHolder.tv_download_num.setText(download);
		}

		// ImageListener listener = ImageLoader.getImageListener(
		// viewHolder.iv_softImage, android.R.drawable.ic_menu_rotate,
		// android.R.drawable.ic_delete);
		//
		// mImageLoader.get(VMConst.URL + activeSoft.getImageUrl(), listener);

		// 显示图片的配置
		// DisplayImageOptions options = new DisplayImageOptions.Builder()
		// .showImageOnLoading(R.drawable.loading)
		// .showImageOnFail(R.drawable.error).cacheInMemory(true)
		// .cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();

		ImageLoader.getInstance().displayImage(
				VMConst.URL + activeSoft.getImageUrl(),
				viewHolder.iv_softImage, MyDisplayImageOptions.getInstance());

		// viewHolder.iv_softImage.setDefaultImageResId(R.drawable.loading);
		// viewHolder.iv_softImage.setErrorImageResId(R.drawable.error);
		// viewHolder.iv_softImage.setImageUrl(
		// VMConst.URL + activeSoft.getImageUrl(), mImageLoader);

		convertView.setOnClickListener(gd_listener);

		new FoundDown(mContext, convertView, activeSoft);

		activeSoft = null;

		return convertView;
	}

	OnClickListener gd_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(v.getContext(), DetailActivity.class);
			TextView textView = (TextView) v.findViewById(R.id.tv_softName);
			String softId = textView.getTag().toString();
			intent.putExtra("softId", softId);
			v.getContext().startActivity(intent);
		}
	};

	static class ViewHolder {
		TextView tv_softName;
		ImageView iv_softImage;
		// NetworkImageView iv_softImage;
		TextView tv_softSize;
		TextView tv_download_num;

	}

}
